#!/bin/bash
# Copyright (C) 2024 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

set -e

failure() {
    test ${#@} -eq 0 || echo "$(basename "$0"):" "$@" >&2
    exit 1
}

usage() {
    echo "usage: $(basename "$0") [OPTION]..."
    echo "Run the CI pipeline or parts of it."
    echo
    echo "  -b, --build                build webpack bundle"
    echo "  -t, --tests                run scss_content tests"
    echo "  -F, --check-format         check for correct formatting"
    echo "  -T, --tsc                  run tsc"
    echo "  -E, --eslint               run eslint"
    echo "  -a, --all                  shortcut for -b -t -F -T -E"
    echo "  -f, --format               format sources"
    echo "  -h, --help                 show this help"
}

parse_options() {
    # Yes, all those option variables are global.
    RUN_BUILD=no
    RUN_TESTS=no
    RUN_CHECK_FORMAT=no
    RUN_TSC=no
    RUN_ESLINT=no
    RUN_FORMAT=no

    if ! OPTIONS=$(getopt --options 'btFTEafh' --long 'build,tests,check-format,tsc,eslint,all,format,help' --name "$(basename "$0")" -- "$@"); then
        usage >&2
        failure
    fi
    eval set -- "$OPTIONS"
    unset OPTIONS

    while true; do
        case "$1" in
            '-b' | '--build')
                RUN_BUILD=yes
                shift
                continue
                ;;
            '-t' | '--tests')
                RUN_TESTS=yes
                shift
                continue
                ;;
            '-F' | '--check-format')
                RUN_CHECK_FORMAT=yes
                shift
                continue
                ;;
            '-T' | '--tsc')
                RUN_TSC=yes
                shift
                continue
                ;;
            '-E' | '--eslint')
                RUN_ESLINT=yes
                shift
                continue
                ;;
            '-a' | '--all')
                RUN_BUILD=yes
                RUN_TESTS=yes
                RUN_CHECK_FORMAT=yes
                RUN_TSC=yes
                RUN_ESLINT=yes
                shift
                continue
                ;;
            '-f' | '--format')
                RUN_FORMAT=yes
                shift
                continue
                ;;
            '-h' | '--help')
                usage
                exit 0
                ;;
            '--')
                shift
                test ${#@} -eq 0 || failure "extra arguments:" "$@"
                break
                ;;
            *) failure "internal error" ;;
        esac
    done

    readonly RUN_BUILD RUN_TESTS RUN_CHECK_FORMAT RUN_TSC RUN_ESLINT RUN_FORMAT
}

# >>> kept here for groovy-lint, see CMK-23740
npm_install_dependencies() {
    npm \
        ci \
        --yes \
        --unsafe-perm \
        --audit=false
}

npm_dependencies_installed() {
    npm ls --all &>/dev/null
}

setup_npm() {
    test "${SETUP_NPM_RAN}" = "yes" && return
    npm_dependencies_installed || npm_install_dependencies
    echo "npm version $(npm --version)"
    echo "node version $(node --version)"
    SETUP_NPM_RAN=yes
}
# <<< kept here for groovy-lint

run_build() {
    # kept here for groovy-lint, see CMK-23740
    setup_npm

    bazel build :dist
}

run_check_format() {
    bazel run :format.check "$(pwd)"
}

run_tsc() {
    bazel test :type-check
}

run_eslint() {
    bazel test :eslint
}

run_format() {
    bazel run :format "$(pwd)"
}

run_tests() {
    bazel test :scss_content
}

main() {
    # Change to the directory where this script resides, it makes many things easier
    # and we can call this script from everywhere.
    cd -- "${BASH_SOURCE%/*}"
    parse_options "$@"
    test ${RUN_BUILD} = yes && run_build
    test ${RUN_TESTS} = yes && run_tests
    test ${RUN_CHECK_FORMAT} = yes && run_check_format
    test ${RUN_TSC} = yes && run_tsc
    test ${RUN_ESLINT} = yes && run_eslint
    test ${RUN_FORMAT} = yes && run_format
    true
}

main "$@"
